package io.renren.modules.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.back.dao.GoodsAttributeDao;
import io.renren.modules.back.dao.GoodsDao;
import io.renren.modules.back.dao.GoodsGalleryDao;
import io.renren.modules.back.dao.ProductDao;
import io.renren.modules.back.entity.GoodsAttributeEntity;
import io.renren.modules.back.entity.GoodsEntity;
import io.renren.modules.back.entity.GoodsGalleryEntity;
import io.renren.modules.back.entity.ProductEntity;
import io.renren.modules.back.service.GoodsService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品service接口实现类
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019年2月11日 上午15:28:55
 */
@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private GoodsGalleryDao goodsGalleryDao;
    @Autowired
    private GoodsAttributeDao goodsAttributeDao;

    @Override
    public List<GoodsEntity> queryList(Map<String, Object> map) {
        return baseMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return baseMapper.queryTotal(map);
    }

    @Override
    @Transactional
    public int saveGoods(GoodsEntity goods) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        Map<String, Object> map = new HashMap<>();
        map.put("name", goods.getName());
        List<GoodsEntity> list = queryList(map);
        if (null != list && list.size() != 0) {
            throw new RRException("商品名称已存在！");
        }
        goods.setIsDelete(0);
        goods.setDeptId(user.getDeptId());
        goods.setCreateUserId(user.getUserId());
        goods.setUpdateUserId(user.getUserId());
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        Integer number = baseMapper.insert(goods);
        Integer id = goods.getId();

        //保存商品详情页面显示的属性
        List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
        if (null != attributeEntityList && attributeEntityList.size() > 0) {
            for (GoodsAttributeEntity item : attributeEntityList) {
                item.setGoodsId(id);
                goodsAttributeDao.insert(item);
            }
        }

        //商品轮播图
        List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
        if (null != galleryEntityList && galleryEntityList.size() > 0) {
            for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
                galleryEntity.setGoodsId(id);
                goodsGalleryDao.insert(galleryEntity);
            }
        }

        return number;
    }

    @Override
    @Transactional
    public int update(GoodsEntity goods) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        Integer id = goods.getId();
        List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
        if (null != attributeEntityList && attributeEntityList.size() > 0) {
            for (GoodsAttributeEntity goodsAttributeEntity : attributeEntityList) {
                int result = goodsAttributeDao.update(goodsAttributeEntity,
                        new QueryWrapper<GoodsAttributeEntity>().eq("goods_id",id).eq("attribute_id",goodsAttributeEntity.getAttributeId())
                );
                if (result == 0) {
                    goodsAttributeEntity.setGoodsId(id);
                    goodsAttributeDao.insert(goodsAttributeEntity);
                }
            }
        }
        goodsAttributeDao.delete(
                new QueryWrapper<GoodsAttributeEntity>().eq("goods_id",id).notInSql("attribute_id","select id from nideshop_attribute where attribute_category_id = " + goods.getAttributeCategory())
        );
        goods.setUpdateUserId(user.getUserId());
        goods.setUpdateTime(new Date());
        //商品轮播图
        //修改时全删全插
        List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
        if (null != galleryEntityList && galleryEntityList.size() > 0) {
            goodsGalleryDao.delete(
                    new QueryWrapper<GoodsGalleryEntity>().eq("goods_id",id)
            );
            for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
                galleryEntity.setGoodsId(id);
                goodsGalleryDao.insert(galleryEntity);
            }
        }
        return baseMapper.updateById(goods);
    }

    @Override
    public int delete(Integer id) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        GoodsEntity goodsEntity = baseMapper.selectById(id);
        goodsEntity.setIsDelete(1);
        goodsEntity.setIsOnSale(0);
        goodsEntity.setUpdateUserId(user.getUserId());
        goodsEntity.setUpdateTime(new Date());
        return baseMapper.updateById(goodsEntity);
    }

    @Override
    @Transactional
    public int deleteBatch(Integer[] ids) {
        int result = 0;
        for (Integer id : ids) {
            result += delete(id);
        }
        return result;
    }
}
